<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="安装位置" prop="location">
        <el-input v-model="queryParams.location" placeholder="请输入安装位置" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!--<el-form-item label="安装日期" prop="installTime">-->
        <!--<el-date-picker clearable size="small" v-model="queryParams.installTime" type="date" value-format="yyyy-MM-dd"-->
          <!--placeholder="选择安装日期">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="责任人" prop="responsible">
        <el-input v-model="queryParams.responsible" placeholder="请输入责任人" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="respPhone">
        <el-input v-model="queryParams.respPhone" placeholder="请输入责任人电话" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['environment:opmEnvFacility:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['environment:opmEnvFacility:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['environment:opmEnvFacility:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['environment:opmEnvFacility:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="opmEnvFacilityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="设施编号" align="center" prop="buildNum" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="所属园区" align="center" prop="parkNum" />
      <el-table-column label="安装位置" align="center" prop="location" />
      <el-table-column label="安装日期" align="center" prop="installTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.installTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分类" align="center" prop="deviceType" />
      <!--<el-table-column label="规格型号" align="center" prop="specification" />-->
      <!--<el-table-column label="生产厂商" align="center" prop="supplier" />-->
      <!--<el-table-column label="工作状态" align="center" prop="status">-->
        <!--<template slot-scope="scope">-->
          <!--<span>{{getChangeType(scope.row.status)}}</span>-->
        <!--</template>-->
      <!--</el-table-column>-->
      <el-table-column label="责任人" align="center" prop="responsible" />
      <el-table-column label="联系电话" align="center" prop="respPhone" />
      <el-table-column label="设施说明" align="center" prop="desc1" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['environment:opmEnvFacility:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['environment:opmEnvFacility:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改园区设施对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园区" prop="parkNum">
          <el-select clearable v-model="form.parkNum" placeholder="请选择园区">
            <el-option v-for="item in buiParkList" :key="item.id" :label="item.parkNum" :value="item.parkNum">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="设施编号" prop="buildNum">
          <el-input v-model="form.buildNum" placeholder="请输入设施编码" disabled/>
        </el-form-item>
        <el-form-item label="分类" v-if="!form.id">
          <treeselect v-model="form.sysBmId" :options="sysBmTypeOptions" :normalizer="bmNormalizer"
                      @select="genCode" placeholder="物资分类"/>
        </el-form-item>
        <!--<el-form-item label="规格型号" prop="specification">-->
          <!--<el-input v-model="form.specification" placeholder="请输入规格型号" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="生产厂商" prop="supplier">-->
          <!--<el-select v-model="form.supplier" filterable placeholder="请选择生产厂商">-->
            <!--<el-option-->
              <!--v-for="item in supplierList"-->
              <!--:key="item.id"-->
              <!--:label="item.name"-->
              <!--:value="item.name"-->
            <!--/>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="工作状态" prop="status">-->
          <!--<el-select v-model="form.status" placeholder="请选择工作状态">-->
            <!--<el-option v-for="item in statusList" :key="item.val" :label="item.label"-->
              <!--:value="item.val">-->
            <!--</el-option>-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <el-form-item label="安装位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入安装位置" />
        </el-form-item>
        <el-form-item label="安装日期" prop="installTime">
          <el-date-picker clearable size="small" v-model="form.installTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入责任人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="respPhone">
          <el-input v-model="form.respPhone" placeholder="请输入责任人电话" />
        </el-form-item>
        <el-form-item label="设施说明" prop="desc1">
          <el-input v-model="form.desc1" placeholder="请输入设施说明" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listOpmEnvFacility,
    getOpmEnvFacility,
    delOpmEnvFacility,
    addOpmEnvFacility,
    updateOpmEnvFacility
  } from "@/api/smartParks/environment/opmEnvFacility";
  // import {listWySupplier} from "@/api/smartParks/wySupplier/wySupplier";
  import { listBuiPark } from "@/api/parkAssets/enterCommunity";
  export default {
    name: "OpmEnvFacility",
    components: {},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 园区设施表格数据
        opmEnvFacilityList: [],
        //supplierList:[],
        buiParkList:[],
        sysBmTypeOptions:this.$store.state.permission.sysBmFacilityTree,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          classify:'1',
          name: null,
          location: null,
          installTime: null,
          responsible: null,
          respPhone: null,
        },
        //statusList:[{label:'正常',val:'0'},{label:'异常',val:'1'}],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [{
            required: true,
            message: "名称不能为空",
            trigger: "blur"
          }],
          parkNum: [{
            required: true,
            message: "园区不能为空",
            trigger: "blur"
          }],
          // specification: [{
          //   required: true,
          //   message: "规格型号不能为空",
          //   trigger: "blur"
          // }],
          // status: [{
          //   required: true,
          //   message: "工作状态不能为空",
          //   trigger: "change"
          // }],
          location: [{
            required: true,
            message: "安装位置不能为空",
            trigger: "blur"
          }],
          installTime: [{
            required: true,
            message: "安装日期不能为空",
            trigger: "blur"
          }],
          responsible: [{
            required: true,
            message: "责任人不能为空",
            trigger: "blur"
          }],
        }

      };
    },
    created() {
      this.getList();
      // listWySupplier().then((response) => {
      //   this.supplierList = response.rows;
      // });
      listBuiPark().then((response) => {
        this.buiParkList = response.rows;
      });
    },
    methods: {
      /** 查询园区设施列表 */
      getList() {
        this.loading = true;
        listOpmEnvFacility(this.queryParams).then(response => {
          this.opmEnvFacilityList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      genCode(item){
        this.form.sysBmId=item.bmId
      },
      getChangeType(e) {
        for (var i = 0; i < this.statusList.length; i++) {
          if (this.statusList[i].val == e) {
            return this.statusList[i].label;
          }
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: '',
          buildNum: null,
          specification: null,
          classify:'1',
          status: null,
          supplier:null,
          location: null,
          installTime: null,
          responsible: null,
          respPhone: null,
          desc1: null,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加园区设施";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getOpmEnvFacility(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改园区设施";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.classify = '1';
            if (this.form.id != null) {
              updateOpmEnvFacility(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addOpmEnvFacility(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除园区设施编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOpmEnvFacility(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('environment/opmEnvFacility/export', {
          ...this.queryParams
        }, `environment_opmEnvFacility.xlsx`)
      }
    }
  };
</script>
